package com.peng.order;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.peng.order.pojo.dto.CarInfoDto;
import com.peng.pojo.UserInfoVO;
import com.peng.web.JsonResult;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * @author： Peng
 * @date： 2024/6/24
 * @version: 1.0
 */
@SpringBootTest
public class RestTemplateTestCase {
    @Autowired
    private RestTemplate restTemplate;

    @Test
    public void getCarInfo() {
        String url = "http://127.0.0.1:8083/user/info/{1}";
        ParameterizedTypeReference<JsonResult<UserInfoVO>> typeReference = new ParameterizedTypeReference<JsonResult<UserInfoVO>>(){};
        ResponseEntity<JsonResult<UserInfoVO>> entity = restTemplate.exchange(url, HttpMethod.GET, null, typeReference, 1);
//        ResponseEntity<JsonResult> forEntity = restTemplate.getForEntity(url, JsonResult.class, 1);
        if (entity != null) {
            HttpStatus statusCode = entity.getStatusCode();
            if (HttpStatus.OK == statusCode) {
                JsonResult body = entity.getBody();
                System.out.println(body);
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                CarInfoDto carInfoDto = objectMapper.readValue(body.getData().toString(), CarInfoDto.class);
                System.out.println("carInfoDto = " + carInfoDto);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            }
        }
    }

}
